package com.prj.ufdm.auth.security;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.prj.ufdm.auth.bpo._impl.SysFunctionBPOImpl;
import com.prj.ufdm.auth.bpo._impl.SysUserBPOImpl;
import com.prj.ufdm.auth.model.SysUser;

/**
 * 用户登录验证
 * @author Administrator
 */
@Service("userDetailsServiceImpl")
public class UserDetailsServiceImpl implements UserDetailsService  {
	
	@Autowired
	SysUserBPOImpl sysUserBPOImpl;
	
	@Autowired
	SysFunctionBPOImpl sysFunctionBPOImpl;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		SysUser sysUser = null;
		try {
			sysUser = sysUserBPOImpl.doGetSysUserByUsername(username);
			if (sysUser != null) {
				Collection<GrantedAuthority> grantedAuthorities = sysFunctionBPOImpl.doGetGantedAuthoritiestByUserId(sysUser.getId());
				sysUser.setGrantedAuthorities(grantedAuthorities);
			} else {
				throw new UsernameNotFoundException("登录失败");
			}
		} catch (Exception er) {
			er.printStackTrace();
			throw new UsernameNotFoundException("登录异常");
		}
		return sysUser;
	}
	
}
